package com.example.administrator.myapplication.activity;

import android.annotation.SuppressLint;
import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentStatePagerAdapter;
import android.support.v4.app.FragmentTransaction;
import android.support.v4.view.ViewPager;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;

import com.example.administrator.myapplication.R;
import com.example.administrator.myapplication.utils.SharedPreFerencesTools;
import com.example.administrator.myapplication.view.ColorAnimationView;
import com.sv.svbind.annotation.Bind;
import com.sv.svbind.context.AppActivity;

import java.util.LinkedList;

public class HomeActivity extends AppActivity {

    @Bind(id = R.id.ColorAnimationView)
    private ColorAnimationView colorAnimationView;

    @Bind(id = R.id.viewPager)
    private ViewPager viewPager;

    @Bind(id = R.id.content)
    private LinearLayout content;

    private LinkedList<Fragment> fragments;
    private static final int[] resource = new int[]{R.mipmap.xuan1, R.mipmap.k55, R.mipmap.k444, R.mipmap.k004};
    private static final String TAG = HomeActivity.class.getSimpleName();

    private FragmentManager fManager;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_home);


        initView();
        initData();
        viewPagerEvent();
    }

    private void initView() {
        fManager = getSupportFragmentManager();
        viewPager = (ViewPager) findViewById(R.id.viewPager);
        colorAnimationView = (ColorAnimationView) findViewById(R.id.ColorAnimationView);
    }

    private void initData() {
        fragments = new LinkedList<>();

        viewPager.setAdapter(new MyFragmentStatePager(getSupportFragmentManager()));
        colorAnimationView.setmViewPager(viewPager, resource.length);

    }

    private void viewPagerEvent() {
        colorAnimationView.setOnPageChangeListener(new ViewPager.OnPageChangeListener() {

            @Override
            public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {


            }

            @Override
            public void onPageSelected(int position) {
            }

            @Override
            public void onPageScrollStateChanged(int state) {
            }
        });
    }

    private void initFragment(int position, Fragment fragment) {
        FragmentTransaction ft = fManager.beginTransaction();
        ft.replace(R.id.content, fragment);
        ft.commit();
    }


    public class MyFragmentStatePager
            extends FragmentStatePagerAdapter {

        public MyFragmentStatePager(FragmentManager fm) {
            super(fm);
        }

        @Override
        public Fragment getItem(int position) {
            return new MyFragment(position);
        }

        @Override
        public int getCount() {
            return resource.length;
        }
    }


    @SuppressLint("ValidFragment")
    public class MyFragment
            extends Fragment {
        private int position;

        public MyFragment(int position) {
            this.position = position;
        }

        @Override
        public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
            ImageView imageView = new ImageView(getActivity());
            imageView.setScaleType(ImageView.ScaleType.FIT_XY);
            imageView.setImageResource(resource[position]);

            imgEvent(position, imageView);

            return imageView;
        }
    }

    private void imgEvent(int position, ImageView imageView) {
        switch (position) {
            case 0:
                imageView.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        Log.i("ddd","imgEvent--->"+SharedPreFerencesTools.getUserId());
                        if (SharedPreFerencesTools.getUserId()!=null&&!SharedPreFerencesTools.getUserId().equals("")){
                            startActivity(new Intent(HomeActivity.this, ChatActivity.class));
                        }else{
                            startActivity(new Intent(HomeActivity.this, LoginNActivity.class));
                        }

                    }
                });
                break;
            case 1:
                imageView.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        startActivity(new Intent(HomeActivity.this, OActivity.class));
                    }
                });
                break;
            case 2:
                break;
            case 3:
                break;
        }
    }
}
